﻿@{
    Layout = "";
}
@model Nop.Plugin.DiscountRules.CustomerRoles.Models.RequirementModel
@using Nop.Web.Framework;

<script type="text/javascript">
    $(document).ready(function () {
        $('#savecustomerrolesrequirement@(Model.RequirementId)').click(function () {
            var customerRoleId = $("#@Html.FieldIdFor(model => model.CustomerRoleId)").val();
            var discountId = @Model.DiscountId;
            var requirementId = @Model.RequirementId;

            var postData = {
                discountId: discountId,
                discountRequirementId: requirementId,
                customerRoleId: customerRoleId
            };
            addAntiForgeryToken(postData);

            $.ajax({
                cache:false,
                type: "POST",
                url: "@(Url.Action("Configure", "DiscountRulesCustomerRoles"))",
                data: postData,
                success: function (data) {
                    $('#pnl-save-requirement-result@(Model.RequirementId)').fadeIn("slow").delay(1000).fadeOut("slow");
                    //notify parent if it's a new requirement
                    @if (Model.RequirementId == 0)
                    {
                        <text>$("#discountRequirementContainer").trigger('nopnewdiscountruleadded', [data.NewRequirementId]);</text>
                    }
                },
                error:function (xhr, ajaxOptions, thrownError){
                    alert('@Html.Raw(HttpUtility.JavaScriptStringEncode(T("Admin.Promotions.Discounts.Requirements.FailedToSave").Text))');
                }
            });
        });
    });
</script>

<div class="form-group">
    <div class="col-md-3">
        @Html.NopLabelFor(model => model.CustomerRoleId)
    </div>
    <div class="col-md-9">
        @Html.NopDropDownListFor(model => model.CustomerRoleId, Model.AvailableCustomerRoles)
        <div id="pnl-save-requirement-result@(Model.RequirementId)" style="display: none;">
            @T("Admin.Promotions.Discounts.Requirements.Saved")
        </div>
    </div>
</div>
<div class="form-group">
    <div class="col-md-3">
        &nbsp;
    </div>
    <div class="col-md-9">
        <input type="button" id="savecustomerrolesrequirement@(Model.RequirementId)" class="btn btn-primary" value="@T("Admin.Common.Save")" />
    </div>
</div>